<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/18 上午11:50
 * +-------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\SysAuth;
use app\admin\model\SysMenu;
use app\admin\model\SysUploadfile;
use app\admin\service\CategoryService;
use app\common\controller\AdminController;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\Query;
use think\response\Json;

class Select extends AdminController
{

    /**
     * 获取权限列表
     * @return Json
     */
    public function auth(): Json
    {
        return $this->successJson((new SysAuth())->getAuthList());
    }

    /**
     * 获取菜单列表
     * @return Json
     */
    public function menu(): Json
    {
        return $this->successJson((new SysMenu())->getPidMenuList());
    }

    /**
     * 角色权限节点列表
     * @param         $id
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function authorize($id): Json
    {
        $model = new SysAuth();
        $row = $model->find($id);
        if(!$row) return $this->errorJson('数据不存在');
        $list = $model->getAuthorizeNodeListByAdminId($id);
        return $this->successJson($list);
    }

    /**
     * 获取上传文件列表
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function file(Request $request): Json
    {
        $get         = $request->get();
        $page        = !empty($get['page']) ? $get['page'] : 1;
        $limit       = !empty($get['limit']) ? $get['limit'] : 10;
        $title       = !empty($get['title']) ? $get['title'] : null;
        $this->model = new SysUploadfile();
        $count       = $this->model
                ->where(function (Query $query) use ($title) {
                    !empty($title) && $query->where('original_name', 'like', "%{$title}%");
                })->count();
        $list        = $this->model
                ->where(function (Query $query) use ($title) {
                    !empty($title) && $query->where('original_name', 'like', "%{$title}%");
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
        return $this->successJson(compact('count', 'list'));
    }

    /**
     * 获取项目分类
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function projectCate(): Json
    {
        $list  = (new \app\admin\model\ProjectCate())->field('id,pid,title')->select();
        return $this->successJson((new CategoryService())->getTreeData($list));
    }

    /**
     * 获取产品分类
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function productCate(): Json
    {
        $list = (new \app\admin\model\ProductCate())->field('id,pid,title')->select();
        return $this->successJson((new CategoryService())->getTreeData($list));
    }

    /**
     * 获取门店列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function store(): Json
    {
        return $this->successJson((new \app\admin\model\Store())->where('status',1)->select());
    }
}